package viewset.com.recyclewview.threeLayoutManager;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.widget.ImageView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import viewset.com.recyclewview.R;

/**
 * Created by dell on 2017/11/15.
 */

public class GlideUtil {

    private static int placeholder = R.mipmap.tvchannel_placeholder;
    private static int vide0_placeholder = R.mipmap.video_placeholder;

    public enum ScaleType {
        DEFAULT, CENTER_CROP
    }

    private static RequestOptions requestOptions(int resId) {
        return requestOptions(resId, null);
    }

    private static RequestOptions requestOptions(int resId, Transformation transformation) {
        RequestOptions requestOptions = new RequestOptions();
        if (transformation != null) {
            requestOptions.transform(transformation);
        }
        requestOptions.placeholder(resId).error(resId).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.DATA);
        return requestOptions;
    }

    private static RequestOptions requestOptions(Drawable drawable) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(drawable).error(drawable);
        return requestOptions;
    }

    private static int getDimen(Context context, int resId) {
        return context.getResources().getDimensionPixelSize(resId);
    }

    private static int getColor(Context context, int resId) {
        return context.getResources().getColor(resId);
    }

    private static void setRectangleBorder(ImageView imageView, @DimenRes int borderWidthResId, @ColorRes int borderColorResId, @DimenRes int radiusResId) {
        int borderWidth = getDimen(imageView.getContext(), borderWidthResId);
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setStroke(borderWidth, getColor(imageView.getContext(), borderColorResId));
        drawable.setCornerRadius(getDimen(imageView.getContext(), radiusResId));
        imageView.setPadding(borderWidth, borderWidth, borderWidth, borderWidth);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            imageView.setBackground(drawable);
        }
    }

    //设置ImageView圆形外框背景
    private static void setCircleBorder(ImageView imageView, @DimenRes int borderWidthResId, @ColorRes int borderColorResId) {
        int borderWidth = getDimen(imageView.getContext(), borderWidthResId);
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.OVAL);
        drawable.setStroke(borderWidth, getColor(imageView.getContext(), borderColorResId));
        imageView.setPadding(borderWidth, borderWidth, borderWidth, borderWidth);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            imageView.setBackground(drawable);
        }
    }

    /**
     * 圆形加载
     *
     * @param context
     * @param path
     * @param imageview
     */
    public static void loadCircleImageWithborder(Context context, String path,
                                                 ImageView imageview, @DimenRes int borderWidthResId, @ColorRes int borderColorResId) {
        if (context == null) {
            return;
        }
        setCircleBorder(imageview, borderWidthResId, borderColorResId);
        NfwxGlide.with(context).load(path).apply(requestOptions(placeholder).circleCrop()).into(imageview);
    }

    /**
     * 圆形加载
     *
     * @param context
     * @param path
     * @param imageview
     */
    public static void loadCircleImage(Context context, String path,
                                       ImageView imageview) {
        if (context == null) {
            return;
        }
        NfwxGlide.with(context).load(path).apply(requestOptions(placeholder).circleCrop()).into(imageview);
    }

    public static void loadCircleImage(Context context, int resourceId,
                                       ImageView imageview) {
        if (context == null) {
            return;
        }
        NfwxGlide.with(context).load(resourceId).apply(requestOptions(placeholder).circleCrop()).into(imageview);
    }

    public static void loadCircleImageWithborder(Context context, int resourceId,
                                                 ImageView imageview, @DimenRes int borderWidthResId, @ColorRes int borderColorResId) {
        if (context == null) {
            return;
        }
        setCircleBorder(imageview, borderWidthResId, borderColorResId);
        NfwxGlide.with(context).load(resourceId).apply(requestOptions(placeholder).circleCrop()).into(imageview);
    }

    /**
     * 加载图片
     *
     * @param context
     * @param path
     * @param imageView
     */
    public static void loadImage(Context context, String path, ImageView imageView) {
        if (context == null) {
            return;
        }
        NfwxGlide.with(context).load(path).apply(requestOptions(placeholder)).into(imageView);
    }

    public static void loadImage(Context context, String path, ImageView imageView, int width, int height) {
        if (context == null) {
            return;
        }
        if (width != 0 && height != 0) {
            NfwxGlide.with(context).load(path).apply(requestOptions(placeholder).override(width, height)).into(imageView);
        } else {
            NfwxGlide.with(context).load(path).apply(requestOptions(placeholder)).into(imageView);
        }
    }

    /**
     * 加载获取之前一帧的图片
     *
     * @param context
     * @param path
     * @param imageView
     */
    public static void loadImageNotPlace(Context context, String path, ImageView imageView) {
        if (context == null) {
            return;
        }
        Drawable drawable = imageView.getDrawable();
        if (drawable != null) {
            NfwxGlide.with(context).load(path).apply(requestOptions(drawable)).into(imageView);
        } else {
            NfwxGlide.with(context).load(path).into(imageView);
        }
    }

    public static void loadImage(Context context, int rid, ImageView imageView) {
        if (context == null) {
            return;
        }
        NfwxGlide.with(context).load(rid).apply(requestOptions(placeholder)).into(imageView);
    }

    public static void loadSimpleImage(Context context, String path, ImageView imageView) {
        if (context == null) {
            return;
        }
        NfwxGlide.with(context).load(path).apply(requestOptions(placeholder).centerCrop()).into(imageView);
    }


    /**
     * 加载圆角图片
     *
     * @param context
     * @param path
     * @param imageView
     */
    public static void loadRoundImage(Context context, Integer path, ImageView imageView) {
        loadRoundImage(context, path, imageView, R.dimen.dp2, placeholder, ScaleType.CENTER_CROP);
    }

    public static void loadRoundImage(Context context, Integer path, ImageView imageView, ScaleType scaleType) {
        loadRoundImage(context, path, imageView, R.dimen.dp2, placeholder, scaleType);
    }

    public static void loadRoundImage(Context context, Integer path, ImageView imageView, @DimenRes int radiusResId) {
        loadRoundImage(context, path, imageView, radiusResId, placeholder, ScaleType.CENTER_CROP);
    }

    public static void loadRoundImage(Context context, Integer path, ImageView imageView, @DimenRes int radiusResId, int placeholderResId, ScaleType scaleType) {
        if (context == null) {
            return;
        }
        try {
            int radius = getDimen(imageView.getContext(), radiusResId);
            if (scaleType == ScaleType.CENTER_CROP) {
                NfwxGlide.with(context).load(path).apply(requestOptions(placeholderResId, new MultiTransformation<>(new CenterCrop(), new RoundedCornersTransformation(radius, 0)))).into(imageView);
            } else if (scaleType == ScaleType.DEFAULT) {
                NfwxGlide.with(context).load(path).apply(requestOptions(placeholderResId, new MultiTransformation<>(new RoundedCornersTransformation(radius, 0)))).into(imageView);
            }
        } catch (IllegalArgumentException e) {
        }
    }

    /**
     * 加载圆角图片
     *
     * @param context
     * @param path
     * @param imageView
     * @param borderWidthResId
     * @param borderColorResId
     * @param radiusResId
     */
    public static void loadRoundImageWithBorder(Context context, Integer path, ImageView imageView, @DimenRes int borderWidthResId, @ColorRes int borderColorResId, @DimenRes int radiusResId) {
        if (context == null) {
            return;
        }
        setRectangleBorder(imageView, borderWidthResId, borderColorResId, radiusResId);
        loadRoundImage(context, path, imageView, radiusResId, placeholder, ScaleType.CENTER_CROP);
    }
}
